define([ "jquery","mustache.min","comm","site_tip","dialog","space","files/file","files/file_detail","text!/static/page/files.html","text!/static/page/folder_item.html","text!/static/page/content_path.html","files/copy_share","files/policies","files/ctrl_policies"],  function( $,Mustache,comm,Site_tip,Dialog,Space,File,File_detail,FilesTpl,FolderItemTpl,PathTpl,Copy_share,Policies,Ctrl_policies) {
	var site_tip = new Site_tip();
	//相互依赖模块处理
	/*if(!Files)
	{
		Files = require(["files/main"]);
	}*/
	//console.log(Files);
	function Folder(files){
		this.container = $("#mainView");
		this.pathArr = [];
		this.show = false;
		this.addCom = true;
		this.mi_icon_fun = null;
		this.isAdd = false;
		//排序字段
		this.sortkey = "filecreate";
		//排序升序，降序
		this.sortdesc = "desc";
		this.isSort = false;
		this.all_data={};
		this.files = files;
	}
	/**
	 * 重新设定新建文件夹名字索引最大数
	 */
	function resetCreateNewFolderNameMax(cb){
		var curFolderDom = $("#folderPath").find("[uuid][haslast]");
		var params={};
		if(curFolderDom.length>0)
		{
			params.file_id = curFolderDom.attr("uuid");
		}
		$.getJSON("/files/get_new_folder_num",params,function(jsonData){
			//console.log(jsonData);
			if(jsonData.success){
				var arr = jsonData.data;
				var max = parseInt(arr.defindMax);
				/*if(max>=0)
				{
					$("#waterfall").attr("defindMax",max+1);
				}
				else
				{
					$("#waterfall").attr("defindMax",-1);
				}*/
				$("#waterfall").attr("defindMax",max);
			}
			if($.isFunction(cb))
			{
				cb(jsonData);
			}
		});
	};
	Folder.prototype.init = function(bindUI) {
		//$("#waterfall").attr("defindMax",$("#folderDefindMax").val());
		/*var defindMax = $("#waterfall").attr("defindMax");
		if(defindMax > -1){
			defindMax = parseInt(defindMax)+ 1;
			$("#waterfall").attr("defindMax",defindMax);
		}*/
		resetCreateNewFolderNameMax();
		this._bindUI();
		this.defautDetail();
	}

	Folder.prototype.first_init = function()
	{
		this.defautDetail();
		resetCreateNewFolderNameMax();
	}

	Folder.prototype._bindUI = function(bind_detail) {

		//console.log("Folder bind ui");
		var that = this;
		bind_detail = bind_detail=="undefined"?true:bind_detail;

		$("#list-title").off("click");
		//按文件名排序
		$("#list-title").on("click",".icon",function (e){
			var element = $(this).find("span");
			that.setSortCss(element);
			that.sortkey = "filename";
			that.isSort = true;

			var lastTitle = $("#folderPath").find("span[haslast='true']");
			var puuid = lastTitle.attr("uuid");

			$("#fileInfo").remove();
			var e = jQuery.Event("folder.loadNewdir");
			e.file_id = puuid;
			//console.log(lastTitle.attr("title"));
			e.name = lastTitle.attr("title");
			that.container.trigger(e);


		});

		//按文件大小排序
		$("#list-title").on("click",".col-3",function (e){
			var element = $(this).find("span");
			that.setSortCss(element);
			that.sortkey = "filesize";
			that.isSort = true;

			var lastTitle = $("#folderPath").find("span[haslast='true']");
			var puuid = lastTitle.attr("uuid");

			$("#fileInfo").remove();
			var e = jQuery.Event("folder.loadNewdir");
			e.file_id = puuid;
			e.name = lastTitle.attr("title");
			that.container.trigger(e);
		});

		//按上传时间排序
		$("#list-title").on("click",".col-4",function (e){
			var element = $(this).find("span");
			that.setSortCss(element);
			that.sortkey = "filecreate";
			that.isSort = true;

			var lastTitle = $("#folderPath").find("span[haslast='true']");
			var puuid = lastTitle.attr("uuid");

			$("#fileInfo").remove();
			var e = jQuery.Event("folder.loadNewdir");
			e.file_id = puuid;
			e.name = lastTitle.attr("title");
			that.container.trigger(e);
		});

		$("#tool-view").off("click",".icon_addDir");
		$("#tool-view").on("click",".icon_addDir",function(e){
			comm.stopEvent(e);
			var m = $(this);
			/*if(m.attr("disabled"))
			{
				return false;
			}*/
			if(that.addCom){
				that.addCom = false;
				//m.attr("disabled",true);
				that.add();
			}

		});

		$("#mypage").off("click",".folder-icon-outer .icon-del");
		$("#mypage").on("click",".folder-icon-outer .icon-del",function(e){
			//console.log("hahah");
			comm.stopEvent(e);
			var item = $(this).parents(".wfc.item");
			item.find(".icon-checkbox").addClass("hover");
			$("#tool-view .icon_del").trigger("click");
			return;
			var fileId = item.find("input[name='uuid']").val();
			var self = this;
			var del = function(fileId){

				$.getJSON("/files/delete",{folders:fileId},function(jsonData){
					//console.log(jsonData);return;
					if(jsonData.success)
					{
						site_tip.setContent("删除成功",false,true);
						$(self).parents(".wfc.item").remove();
						$("#glable_mask").remove();
						$("#dialog_shade").remove();
						var lastTitle = $("#folderPath").find("span[haslast='true']");
						var puuid = lastTitle.attr("uuid");
						var size = $("#mypage").find(".wfc").size();
						if(size == 0){
							$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
						}
						/*$.getJSON("/files/get_new_folder_num",{file_id:puuid},function(jsonData){
		            		if(jsonData.ret == -1){
		            			var arr = jsonData.data;
		            			$("#waterfall").attr("defindMax",arr.defindMax);
		            		}
		            	});*/
					    resetCreateNewFolderNameMax();
						Space.update(jsonData.data);

						if($("#wfo").data("detail") == 1){
							new Folder().showDetail();
						}


						if($("#waterfall").children().size()<=0)
						{
							//var files = new Files();
							that.files.addEmptyView();
						}

						var oldTotal = parseInt($("#fileTotal").val())||0;
						//console.log("total:"+oldTotal);

						oldTotal =oldTotal==0?0:oldTotal-1;
						that.files.resetCount(oldTotal);

					}
					else{
						site_tip.setContent("删除失败");
					}
				});
				return false;
			}

			var obj = new Object();
			obj.content = "文件夹删除后不可恢复";
			obj.file_id = fileId;

			var dialog = new Dialog(obj, del);
			dialog.init();
			return false;
		});


		/**
		 * 访问控制
		 */
		$("#mypage").off("click",".folder-icon-outer .icon-lock");
		$("#mypage").on("click",".folder-icon-outer .icon-lock",function(e){
			comm.stopEvent(e);
			var item = $(this).parents(".wfc.item");
			item.find(".icon-checkbox").addClass("hover");
			item.addClass("item-hover");
			//file.forbineds("policies");
			require(["files/main"],function(Files){
				new Files().forbineds("policies");
			});
			return false;
			var fileId = item.find("input[name='uuid']").val();
			that.policies.apply(that,[fileId,item]);

		});


		/**
		 * 重命名
		 */
		$("#mypage").off("click",".icon-rename");
		$("#mypage").on("click",".icon-rename",function(e){
			comm.stopEvent(e);
			var parent = $(this).parents(".wfc.item");
			parent.find(".icon-checkbox").addClass("hover");
			var fileId = parent.find("input[name='uuid']").val();
			var self = this;

			var folder = parent.find(".col-1.file");
			folder.hide();

			var filename = folder.attr("title");


			var folderEdit = parent.find(".col-1.fileedit");
			folderEdit.show();

			var input = folderEdit.find("input");
			input.val(filename);
			input.select();


		});

		$("#mypage").on("keydown",".edit-txt",function(e){
			if(e.keyCode==13)
			{
				$(this).blur();
				return false;
			}
		});
		$("#mypage").off("blur",".edit-txt");
		$("#mypage").on("blur",".edit-txt",function(e){
			comm.stopEvent(e);
			var parent = $(this).parents(".wfc.item");

			var lastTitle = $("#folderPath").find("span[hasLast='true']");
			var parentId = lastTitle.attr("uuid");

			var fileId = parent.find("input[name='uuid']").val();

			var fileItem = parent.find(".col-1.file");
			var fileName = fileItem.find(".foldername .foldername-link");

			var fileEdit = parent.find(".col-1.fileedit");
			var newName = $.trim($(this).val());
			//return;
			if(newName.length == 0){
				site_tip.setContent("文件名不能为空",true,false);
				$(this).focus();
				return true;
			}

			if(newName == fileItem.attr("title")){
				fileEdit.hide();
				fileItem.show();
				return true;
			}

			if(!comm.checkSpecialStr(newName)){
				var err_msg = "文件夹名不能包含 \\ / : * ? \" < > |";
				site_tip.setContent(err_msg,true,false);
				return true;
			}

			if(that.isAdd){
				var id = parent.attr("id");
				var index = parent.attr("id").split("_")[1];
				that.addReq(parentId, $(this), index);
			}else{
				that.updateReq(fileId, $(this), fileItem, fileEdit, parent);
			}
		});

		$("#mypage").off("click",".foldername .foldername-link");
		$("#mypage").on("click",".foldername .foldername-link",function(e){
			comm.stopEvent(e);
			that.isSort = false;
			$("#fileInfo").remove();
			var file_id = $(this).parents(".wfc.item").find("input[name=uuid]").val();
			var e = jQuery.Event("folder.loadNewdir");
			e.file_id = file_id;
			e.name = $(this).parents(".col-1").attr("title");
			that.container.trigger(e);
			$("#tool-view").find(".icon_move").show();
			$("#tool-view").find(".icon_control").show();
			$("#wfo").data("last_pid",file_id);
			/*$.getJSON("/folder/getIdByUuid",{file_id:file_id},function(jsonData){
				if(jsonData.ret == -1){
					$("#tool-view").find(".icon_move").show();
					$("#wfo").data("last_pid",jsonData.data.id);
				}
			});*/
		});

		$("#main-title").off("click",".pathItem");
		$("#main-title").on("click",".pathItem",function(e){
			comm.stopEvent(e);
			$("#fileInfo").remove();
			that.isSort = false;
			var file_id = $(this).attr("uuid");
			if(file_id == undefined || file_id == null){
				file_id = "";
			}

			that.pathArr = findPathByUUid(that.pathArr,file_id);

			var e = jQuery.Event("folder.loadNewdir");
			e.file_id = file_id;
			that.container.trigger(e);
			$("#wfo").data("last_pid",file_id);
			/*$.getJSON("/folder/getIdByUuid",{file_id:file_id},function(jsonData){
				if(jsonData.ret == -1){
					$("#wfo").data("last_pid",jsonData.data.id);
				}
			});*/

		});

		that.isShowDetail();



		$("#mypage").off("mouseenter",".wfc[data-type='folder']");
		$("#mypage").on("mouseenter",".wfc[data-type='folder']",function(e){
			if(!isSelectMore($(this))){
				return;
			}
			if(that.is_showIconList()){
				return;
			}
			if(that.mi_icon_fun != null){
				clearInterval(that.mi_icon_fun);
				that.mi_icon_fun = null;
			}

			that.mi_icon_fun = setTimeout(that._show_mi_icon.apply(that,[true,$(this)]),200);
		});


		/**
		 * 鼠标移动到文件夹的事件
		 */
		$("#mypage").off("mouseleave",".wfc[data-type='folder']");
		$("#mypage").on("mouseleave",".wfc[data-type='folder']",function(e){
			if(that.is_showIconList()){
				return;
			}
			if(that.mi_icon_fun != null){
				clearInterval(that.mi_icon_fun);
				that.mi_icon_fun = null;
			}
			that.show_mi_icon(false,$(this));
		});

		/**
		 * 文件夹分享链接
		 */
		$("#mypage").off("click",".folder-icon-outer .icon-share");
		$("#mypage").on("click",".folder-icon-outer .icon-share",function(e){
			var item = $(this).parents(".wfc");
			/*if(item.attr("isforbin") > 0){
				site_tip.setContent("该目录包括禁止访问文件，请开启",true,false);
				return;
			}*/
			//console.log("sssss");
			var uuid = item.find("input[name='uuid']").val();
			var name = item.find("div:eq(0)").attr("title");

			var copy_share = new Copy_share();
			var obj = new Object();
			obj.c_uuid = uuid;
			obj.ctype = 2;
			var list = new Array(obj);
			copy_share.showUI(list,name);
		});


		/***
		 * 选择文件夹复选框
		 */
        $("#mypage").off("check",".icon-checkbox").on("check",function(e,forceChecked){
            if(forceChecked!="undefined")
            {

            }
        });
		$("#mypage").off("click",".icon-checkbox");
		$("#mypage").on("click",".icon-checkbox",function(e,parentClick){
			comm.stopEvent(e);
			var item = $(this).parent().parent().parent();
            var lastActive = $("#mypage").find(".last-active");
			//console.log("1");
			if($(this).hasClass("hover")){
				$(this).removeClass("hover");
				item.removeClass("item-hover");
			//	console.log("2");
                if(item.hasClass("last-active"))
                {
                    item.removeClass("last-active");
                }
				$("#selAllFiles").find(".icon-checkbox").removeClass("hover");

				var folders = $("#mypage").find(".wfc[data-type='folder']");
				var selChkboxs = folders.find(".icon-checkbox.hover:visible");
				if(selChkboxs.length > 0){
					$("#tool-view").find(".icon_move").hide();
					$("#tool-view").find(".icon_control").hide();
				}else{
					$("#tool-view").find(".icon_move").show();
					$("#tool-view").find(".icon_control").show();
				}
				if($("#wfo").data("detail") == 1){
					that.showDetail();
				}
				return false;
			}

            if(e.shiftKey)
            {
                //console.log("多选");
                var chk=$(""),chkItem=$("");
                //var start = lastActive.length>0?lastActive:item;
                var prev = item.prevUntil(".item-hover");
                //console.log(prev.prev());return;
                if(prev.length>0 && prev.prev().hasClass("item-hover"))
                {
                    //var mPrev=0;//向上是否查找到已经选择的行
                    prev.each(function(){
                        var me=$(this);
                        //console.log(me.children().children().children().children());
                        var c = me.children().children().children(".icon-checkbox");
                        if(c.length>0)
                        {
                            chk = chk.add(c[0]);
                            chkItem = chkItem.add(me[0]);
                        }
                    });

                }

                if(chk && chk.length>0)
                {
                    chk.addClass("hover");
                    chkItem.addClass("item-hover");
                    item.nextAll(".item-hover").each(function(){
                        $(this).removeClass("item-hover").children().children().children(".icon-checkbox").removeClass("hover");
                    });
                }
                else
                {
                    var next = item.nextUntil(".last-active");
                    //console.log(next);
                    if(next.length>0 && next.next().hasClass("item-hover"))
                    {
                        next.each(function(){
                            var me=$(this);
                            var c = me.children().children().children(".icon-checkbox");
                            if(c.length>0)
                            {
                                chk = chk.add(c[0]);
                                chkItem = chkItem.add(me[0]);
                            }
                        });
                    }
                    if(chk && chk.length>0)
                    {
                        chk.addClass("hover");
                        chkItem.addClass("item-hover");
                        item.prevAll(".item-hover").each(function(){
                            $(this).removeClass("item-hover").children().children().children(".icon-checkbox").removeClass("hover");
                        });
                    }
                }
                /*if(chk.length<=0)
                {
                    var next = item.nextAll();
                    //console.log(next);
                    if(next.length>0)
                    {
                        next.each(function(){
                            var me=$(this);
                            //console.log(me.children().children().children().children());
                            var c = me.children().children().children(".icon-checkbox");
                            if(c.hasClass("hover"))
                            {
                                return false;
                            }
                            if(c.length>0)
                            {
                                chk = chk.add(c);
                                item1 = item1.add(me);
                            }
                        });
                    }

                }*/


            }
            lastActive.removeClass("last-active");
            $(this).addClass("hover");
            item.addClass("item-hover").addClass("last-active");

			var len = $("#mypage").find(".icon-checkbox:visible").size();
			var selLen = $("#mypage").find(".icon-checkbox.hover:visible").size();

			if(selLen>0 && len == selLen){
				$("#selAllFiles").find(".icon-checkbox").addClass("hover");
			}else{
				$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
			}

			if(selLen > 1){
				$("#mypage").find(".icon-view").hide();
				$("#mypage").find(".folder-icon-view").hide();
			}

			var folders = $("#mypage").find(".wfc[data-type='folder']");
			var selChkboxs = folders.find(".icon-checkbox.hover:visible");
			if(selChkboxs.length > 0){
				$("#tool-view").find(".icon_move").hide();
				$("#tool-view").find(".icon_control").hide();
			}else{
				$("#tool-view").find(".icon_move").show();
				$("#tool-view").find(".icon_control").show();
			}

			if(!parentClick && $("#wfo").data("detail") == 1){
				that.showDetail();
			}
            return false;
		});

		var e = jQuery.Event("folder.bindUI");
		$("#mainView").trigger(e);
	}


	Folder.prototype.is_showIconList = function (){
		var len= $("#fileItem_iconList").length;
		if(len>0){
			return true;
		}
		return false;
	}

	Folder.prototype._show_mi_icon = function(flag,item) {
		var that = this;
		return function() {
			that.show_mi_icon(flag,item);
		}
	}

	Folder.prototype.show_mi_icon = function(flag,item) {
		if(flag){
			item.find(".folder-icon-view").css("display","block");
			return;
		}
		item.find(".folder-icon-view").css("display","none");
	}

	Folder.prototype.add = function() {
		var wfcArr = $("#waterfall").find(".wfc");
		var firstIndex = parseInt(wfcArr.first().attr("f-index"));
		var lastIndex = parseInt(wfcArr.last().attr("f-index"));
		var index = 0;
		if(firstIndex > lastIndex){
			index = firstIndex + 1;
		}else{
			if(isNaN(lastIndex)){
				lastIndex = 0;
			}
			index = lastIndex + 1;
		}
		var view = {"data":[{
			"index" : index
		}]};
		var that = this;
		var item_text = Mustache.to_html(FolderItemTpl, view);

		$("#waterfall").prepend(item_text);
		$("#wfc_"+index).find(".col-1.file").hide();

		var fileEdit = $("#wfc_"+index).find(".col-1.fileedit");
		fileEdit.show();

		var input = fileEdit.find("input");
		input.focus();

		var defindMax = $("#waterfall").attr("defindMax");
		defindMax = parseInt(defindMax)||0;
		if(defindMax <= 0){
			input.val("新建文件夹");
		}else{
			input.val("新建文件夹("+ defindMax+")");
		}
		defindMax++;
		$("#waterfall").attr("defindMax",defindMax);
		input.select();

		that.isAdd = true;
		require(["files/main"],function(Files){
			var files = new Files();
			files.listToTop();
			files.removeEmptyView();
		});
		//console.log(Files);
		//var file = new Files();
		//console.log(that.files);

		return false;
	}
	Folder.prototype.addLoading = function()
	{
		if($("#nameView .list-loading-box").length<=0)
		{
			$("#mainView").html('<div id="data-loading-box"><div class="leftColumn"><div class="main-title clearfix"></div><div class="main"><div class="list"><div class="row item-border list-title "></div><div class="list-loading-box">正在加载...</div></div></div></div></div>');
		}
	}

	/**
	 * 获取列表
	 */
	Folder.prototype.get_all = function(file_id,prevID,isFirstLoad) {

		//console.log(fdName);
		file_id  = file_id || 0;
		//console.log("哈哈哈哈");
		var that = this;
		//prevID = prevID!="undefined"?prevID:that.getCurrentId();
		if(that.cdProcess)
		{
			that.cdProcess.abort();
		}
		var params = new Object();
		//params.file_id = file_id;
		params.colName = that.sortkey;
		params.sortDesc = that.sortdesc;
		var type = comm.getQueryString("type");
		params.type = type;
		//type为1，已下载。2时，已过期
		/*if(type == 1 || type == 2){
			console.log("asdfasdfasdfasdf");
			$.get("/static/page/files.html",function(tmpl){
				$("#mainView").prepend(tmpl);
				that.initSortCss();

				var height = $(window).height() - parseInt($('.wf_header').height()-2);
				var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
				$("#mypage").height(mypageHeight);

				var e = jQuery.Event("folder.bindUI");
				$("#mainView").trigger(e);
				that._bindUI();
				that.creatPath();

			});
			return true;

		}*/
		//console.log("folder");
		//console.log($("#list_loading"));
		that.addLoading();
		//that.removeEmptyView();
		that.cdProcess = $.getJSON("/files/cd/"+file_id,params,function(ret){
			if(ret.success)
			{
				var data = ret['data'];
				that.all_data=data;
				$("#data-loading-box").remove();
				$("#mainView").prepend(FilesTpl);
				that.initSortCss();
				/*var height = $(window).height() - parseInt($('.wf_header').height()-2);
				 var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
				 $("#mypage").height(mypageHeight);*/
				if(data['folders'])
				{
					var item_text = Mustache.to_html(FolderItemTpl, {data:data['folders']});
					$("#waterfall").prepend(item_text);
				}
				var defindMax = parseInt(data['new_folder_num']);
				$("#waterfall").attr("defindMax",defindMax);
				that._bindUI();
				if(isFirstLoad)
				{
					that.defautDetail();
				}
				//return;
				var folder = data.folder;

				//console.log($data);
				/*if(folder && !that.isSort){
					that.pathArr.push(createPathObj(folder.name,folder.uuid));
				}*/
				that.pathArr = [];
				if(folder)
				{
					var path_name = folder.path_name?folder.path_name.split(","):"";
					var path_id = folder.path_id?folder.path_id.split(","):"";
					var path_count = path_id.length;
					if(path_count>0)
					{
						for(var a=0;a<path_count;a++)
						{
							that.pathArr.push(createPathObj(path_name[a],path_id[a]));
						}
					}
					that.pathArr.push(createPathObj(folder.name,folder.uuid));
				}


				/*else if(file_id && !that.isSort)
				 {
				 that.pathArr.push(createPathObj(file_id,fdName));
				 }*/
				that.creatPath();
			}
			else
			{
				var listLoadBox = $("#mainView .list-loading-box");
				if(ret.error_code==7100)
				{
					location.hash="folder_id=0";
					listLoadBox.html(ret.message+",3秒后跳转到根目录");
					setTimeout(function(){
						that.get_all(0);
					},3000);

					return false;
				}


				var goback = (prevID !=undefined && prevID!=file_id)?"<a class='goback' href='javascript:void(0);' style='display: inline;'>返回</a>或者":"";
				listLoadBox.html(ret.message+",请尝试"+goback+"<a href='javascript:void(0);' class='refresh'  style='display: inline;'>刷新</a>操作。");
				listLoadBox.find(".goback").click(function(){
					prevID = prevID || 0;
					that.get_all(prevID,prevID);
				});
				listLoadBox.find(".refresh").click(function(){
					that.get_all(file_id);
				});
			}

		});
		/*return;
		$.getJSON("/folder",params,function(jsonData){
			if(jsonData.ret == -1){
				var defindMax = jsonData.defindMax;
				if(defindMax == undefined || defindMax == null){
					defindMax = -1;
				}
				$.get("/static/page/files.html",function(tmpl){
					$("#mainView").prepend(tmpl);
					that.initSortCss();

					var height = $(window).height() - parseInt($('.wf_header').height()-2);
					var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
					$("#mypage").height(mypageHeight);

					var e = jQuery.Event("folder.bindUI");
					$("#mainView").trigger(e);

					$.get("/static/page/folder_item.html", function(tmpl) {
						var item_text = Mustache.to_html(tmpl, jsonData);
						$("#waterfall").prepend(item_text);

						$("#waterfall").attr("defindMax",defindMax);
						if(defindMax > -1){
							defindMax = parseInt(defindMax)+ 1;
							$("#waterfall").attr("defindMax",defindMax);
						}

						that._bindUI();

						var folder = jsonData.folder;
						if(folder != undefined && !that.isSort){
							that.pathArr.push(createPathObj(folder.name,folder.uuid));
						}


						that.creatPath();





					});
					return false;
				});
				return false;
			}
		});*/
		return false;
	}
    Folder.prototype.getCurrentId = function(){
		var curFolderDom = $("#folderPath").find("[uuid][haslast]");
		var curID=curFolderDom.attr("uuid")||0;
		return curID;
	}
	Folder.prototype.reload = function(){
		var id = this.getCurrentId();
		this.get_all(id);
	}


	/**
	 * 创建路径
	 */
	Folder.prototype.creatPath = function() {
		var that = this;
		var view = {"data":showPathArr(this.pathArr)};
		var item_text = Mustache.to_html(PathTpl, view);

		$("#folderPath").append(item_text);

		var type = comm.getQueryString("type");
		if(type == 1){
			$("#folderPath").find(".navtype").html("已下载");
		}else if(type == 2){
			$("#folderPath").find(".navtype").html("已过期");
		}
//			that._bindUI();
		var e = jQuery.Event("folder.loadcom");
		e.sortkey = that.sortkey;
		e.sortdesc = that.sortdesc;
		e.all_data = that.all_data;
		that.container.trigger(e);
		//console.log(that.container);
		return false;
	}
	
	
	Folder.prototype.update = function() {
		if($("#waterfall").height()<$("#mypage").height()-5){
			var size = $(".wfc").size();
			var c_height = (size+1)*50;
			if(c_height>$("#mypage").height()){
				$("#waterfall").height($("#mypage").height());
			}else{
				$("#waterfall").height(c_height);
			}
		}
		if($("#waterfall").height()>=$("#mypage").height()){
			$("#waterfall").height("100%");
		}
		//$("#mypage").mCustomScrollbar("update");
	}
	
	/**
	 * 添加文件夹请求
	 */
	Folder.prototype.addReq = function(file_id,element,index) {
		var that = this;
		var fileName = $.trim(element.val());
		site_tip.loadingMsg("正在保存...");
		$.getJSON("/files/add_folder",{uuid:file_id,name:fileName},function(jsonData){
			if(jsonData.success){
				that.isAdd = false;
				site_tip.setContent("创建文件夹成功",false,true);
				$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
				$("#wfc_"+index).remove();
				var folder = jsonData.data;
				var view = {"data":[{
					"index" : index,
					"name":fileName,
					"created_ts":comm.formatDate(parseInt(folder.creation_ts)*1000),
					"id":folder.id,
					"access_denied":folder.access_denied
				}]};
				//console.log(view);return;
				var item_text = Mustache.to_html(FolderItemTpl, view);

				$("#waterfall").prepend(item_text);
				that.update();

				/*var tempdir;

				 var defindMax = $("#waterfall").attr("defindMax");
				 if(defindMax == 0){
				 tempdir = "新建文件夹";
				 }else{
				 tempdir = "新建文件夹("+ defindMax+")";
				 }
				 console.log(tempdir+"==="+fileName);
				 if(tempdir == fileName){
				 defindMax = parseInt(defindMax)+ 1;
				 $("#waterfall").attr("defindMax",defindMax);
				 }*/
				resetCreateNewFolderNameMax(function(){
					//$("#tool-view .icon_addDir").attr("disabled",false);
					that.addCom = true;
				});



				if($("#wfo").data("detail") == 1){
					that.showDetail();
				}
				var oldTotal = parseInt($("#fileTotal").val())||0;
				oldTotal ++;
				//var files = new Files();
				that.files.initDrop();
				that.files.resetCount(oldTotal);

			}
			else{
				site_tip.setContent(jsonData.message,true,false);
				element.focus();
			}
		});
		return false;
	}
	
	/**
	 * 更新文件夹请求
	 */
	Folder.prototype.updateReq = function(fileId,element,fileItem,fileEdit,parent) {
		var that=this;
		var el_val = $.trim(element.val());
		site_tip.loadingMsg("正在保存...");
		$.getJSON("/files/folder_rename",{id:fileId,name:el_val},function(jsonData){
			if(jsonData.success){
				site_tip.setContent("重命名成功",false,true);
				fileEdit.hide();
				fileItem.show();
				
				var fileName = fileItem.find(".foldername .foldername-link");
				fileName.text(el_val);
				parent.attr("title",el_val);
				parent.find("div:eq(0)").attr("title",el_val);
				//右侧信息也要更改
				$('#fileInfo').find('.f-name').text(el_val).attr('title', el_val);
				
				var lastTitle = $("#folderPath").find("span[haslast='true']");
				var puuid = lastTitle.attr("uuid");
				resetCreateNewFolderNameMax();
			}
			else
			{
				site_tip.setContent(jsonData.message,false,true);
				if(jsonData.error_code==7100 || jsonData.error_code==7001)
				{
					that.reload();
				}
			}

		});
		return false;
	}
	
	/**
	 * 访问控制
	 */
	Folder.prototype.policies = function(fileId,item) {
		var that = this;
		$.get("/policies", {
			file_id : fileId,type:1
		}, function(tdata) {
			if (!tdata.success) {
				site_tip.setContent(tdata);
				return;
			}
			tdata = tdata.data;
			//console.log(tdata.isOpen);
			if (!tdata.isOpen) {
				var ctrl_policies = new Ctrl_policies(1,fileId);
				ctrl_policies.type = 1;
				ctrl_policies.item = item;
				ctrl_policies.init(tdata);
				return;
			}
			var policies = new Policies();
			policies.type = 1;
			policies.init(tdata);
			
		}, 'json');
		return false;
	}
	
	//设置排序css
	Folder.prototype.setSortCss = function (element){
		if(element.css("display") == "none"){
			$("#list-title").find(".sort").hide();
			this.sortdesc = "asc";
			element.addClass("asc");
		}else{
			$("#list-title").find(".sort").hide();
			if(element.hasClass("asc")){
				element.removeClass("asc");
				element.addClass("desc");
				this.sortdesc = "desc";
			}else{
				element.removeClass("desc");
				element.addClass("asc");
				this.sortdesc = "asc";
			}
		}
		element.css("display","inline-block");
	}
	
	//设置排序css
	Folder.prototype.initSortCss = function (){
		var element = $("#list-title").find(".col-4").find("span");
		if(this.sortkey == "filename"){
			element = $("#list-title").find(".icon").find("span");
		}else if(this.sortkey == "filesize"){
			element = $("#list-title").find(".col-3").find("span");
		}
		element.addClass(this.sortdesc);
		element.css("display","inline-block");
	}
	
	/**
	 * 获取右侧栏详细信息
	 */
	Folder.prototype.showDetail = function() {
		$("#main-title").find(".detail").addClass("hover");
		var that = this;
		var list = comm.getSelFiles();
		var selChkboxs = comm.getChkboxs(list);
		var detail
		//console.log(selChkboxs.length);
		if(selChkboxs.length == 1){
			var item = selChkboxs.parent().parent().parent();
			var uuid = item.find("input[name='uuid']").val();
			var type = 0;
			if(item.attr("data-type")=="folder"){
				type = 1;
			}
			detail = new File_detail();
			detail.init(uuid, type);
			
			return;
		}

		detail = new File_detail();
		detail.showPjt = true;
		detail.selChkLen = selChkboxs.length;
		detail.init();
		return;
	}
	
	
	/**
	 * 获取当前文件夹所有隶属文件和文件夹
	 */
	Folder.prototype.get_all_total = function() {
		var pid = $("#wfo").data("last_pid");
//		$.getJSON("/files/getAllTotalByPid",{pid:pid},function(jsonData){
//			var total = 
//		});
		
	}
	
	
	/**
	 * 创建路径对象
	 */
	function createPathObj(name,uuid)
	{
		var obj = new Object();
		obj.name = name;
		obj.uuid = uuid;
		return obj;
	}
	
	/**
	 * 根据路径的uuid查找元素
	 */
	function findPathByUUid(arr,uuid){
		if(uuid == ""){
			arr = [];
		}
		for(var i = 0, len = arr.length; i < len; i++) {
		    if(arr[i].uuid == uuid){
		    	//截取掉选择文件夹和选择文件夹后面的文件夹
		    	arr.splice(i,len);
		    	break;
		    }
		}
		return arr;
	}
	
	/**
	 * 展示的文件夹路径
	 */
	function showPathArr(arr){
		//console.log(arr);
		var data = new Object();
		var len = arr.length;
		if(len == 0){
			data.noLink = true;
			return data;
		}
		
		if(len == 1){
			data.list = arr;
			var obj = new Object();
			obj.uuid = "";
			data.prePath = obj;
			arr[len-1].noLink = true;
			arr[len-1].last = true;
			return data;
		}
		
		for(var i=0;i<len;i++){
			delete arr[i].noLink;
			delete arr[i].last;
			delete arr[i].arrow;
		}
		if(len <= 3){
			data.prePath = arr[len-2];
			arr[len-1].noLink = true;
			arr[len-1].last = true;
			data.list = arr;
			return data;
		}
		
		var list = new Array();
		
		var eplise = new Object();
		eplise.name = "...";
		eplise.noLink = true;
		eplise.arrow = true;
		
		list.push(eplise);
		arr = arr.slice(len - 3, len);
		
		len = arr.length;
		
		arr[len-1].noLink = true;
		arr[len-1].last = true;
		
		data.prePath = arr[len-2];
		
		list = list.concat(arr);
		data.list = list;
		return data;
	}
	
	
	function isSelectMore(item){
		var selFiles = $("#mypage").find(".wfc");
		var selChkboxs = selFiles.find(".icon-checkbox.hover:visible");
		if(selChkboxs.length > 1){
			return false;
		}
		if(selChkboxs.length == 1){
			var chkItem = selChkboxs.parent().parent().parent();
			var chkInput = chkItem.find("input[name='uuid']");
			var selInput = item.find("input[name='uuid']");
			if($.trim(chkInput.val()) == $.trim(selInput.val())){
				return true;
			}
			return false;
		}
		return true;
	}
	
	Folder.prototype.defautDetail = function () {
		$("#wfo").data("detail",1);
		var detail = $("#main-title").find(".detail");
		detail.addClass("hover");
		this.showDetail();
		detail.attr("title","隐藏详细信息");
	}
	
	Folder.prototype.isShowDetail = function(){
		var that = this;
		$("#main-title").on("click",".detail",function(e){
			var flag = $("#wfo").data("detail");
            var toTop = $("#listToTop");
			if(flag == undefined || flag == 0){
                toTop.addClass("expand");
				$("#wfo").data("detail",1);
				$(this).addClass("hover");
				
				that.showDetail();
				$(this).attr("title","隐藏详细信息");
				$("#waterfall").find(".no-search").css("right",248);
				$("#mypage.empty2").removeClass("empty2").addClass("empty");
				return;
			}
            toTop.removeClass("expand");
			$("#wfo").data("detail",0);
			$(this).removeClass("hover");
			$("#fileInfo").remove();
			$(this).attr("title","显示详细信息");
			$("#waterfall").find(".no-search").css("right",0);
			$("#mypage.empty").removeClass("empty").addClass("empty2");
			return;
		});
	}
	return Folder;
});